% try to estimate the freq before by trying several out and fitting 
% curve to the residual error
clear
results='results'; % dir for results
mkdir(results);
vals=[-100 0 100] ; % list of freqs to test
disp(vals)
CCplt = zeros(size(vals)); % array to hold fits metrics
CCplt(:)=NaN;
CCidx = 0;
reso=64;
NIT=2; % only one plcg step is needed
for the_val=vals % run first stage of plcg at range of freqs 
    CCidx = CCidx +1;
    offr = the_val; % try various frequency offsets
    fn = [results '/' 'try' num2str(the_val) '.pdf'];
    disp(fn)
    tempstr=['using freq of ' num2str(offr)];
    disp(tempstr)
    ruun
    CCplt(CCidx) = model.CC(2); % save the fit metric for each test freq
    figure(665);
    plot(vals,CCplt,'x') 
    % print(gcf,'-dpdf',fn) % save an image of the results
end
P = polyfit(vals,CCplt,2); % fit a parabola to the fit metrics
vallin=linspace(vals(1),vals(end)); % add a lot more evaluation points for parab 
Y = polyval(P,vallin); % eval parabola at lots of points
hold on
plot(vallin,Y,'r') % display fit 
drawnow
hold off
[ymin,yminidx]=min(Y); % brain dead minima search
offr=vallin(yminidx);  % get the freq at the minima
tempstr=['using freq of ' num2str(offr)];
disp(tempstr)
NIT=120;
reso=70;
run0019 % run the full pclg to find the final result